import Vue from "vue";
import Vuex from "vuex";
import axios from "../utils/axios";
Vue.use(Vuex);
const store = new Vuex.Store({
  state: {
    //是否打开窗口
    Windows: {
      contractEditWindow: false,
      roomEditWindow: false,
      roomReplaceWindow: false
    },
    // 是否生成Menu
    isGenerate: false,
    admin:localStorage.admin ?JSON.parse(localStorage.admin):null,
    // 登录用户
    user: localStorage.user ? JSON.parse(localStorage.user) : null
  },
  getters: {
    isLogin: state => {
      // console.log(state.user,Boolean(state.user));
      return Boolean(state.user);
    },
    // 是否是内置管理员
    isAdmin:state=>{
        return Boolean(state.admin);
    }
  },
  mutations: {
    changeUser(state, obj) {
        if(obj){
            state.user = obj;
            localStorage.user = JSON.stringify(obj);
        }else{
            state.user = null;
            localStorage.removeItem("user");
        }
    },
    changeAdmin(state,obj){
        if(obj){
            state.admin = obj;
            localStorage.admin = JSON.stringify(obj);
        }else{
            state.admin = null;
            localStorage.removeItem("admin");
        }
    },
    changeGenerate(state, obj) {
      state.isGenerate = obj;
    },
    // 更改window
    changeWindow(state, obj) {
      state.Windows[obj.name] = obj.val;
    },
    // 注销
    cancel(state) {
        axios.defaults.headers.common["token"] = null;
        axios.defaults.headers.common["authorization"] = null;
        state.user = {};
        localStorage.clear();
    }
  }
});

export default store;
